package com.risk.kernel.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

/**
 * @author: Liwen
 * @createTime: 2025/1/26
 * @description:
 */
@Slf4j
public class JdbcUtil {
    private static Connection conn = null;
    private static PreparedStatement pst = null;
    private static Statement statement = null;

    private static String dbName = "";
    private static String host = "";
    private static String port = "";
    private static String userName = "";
    private static String password = "";

    public static Connection getJdbcConn() {
        String url = "jdbc:mysql://host:port/dbName?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf-8";
        String jdbcUrl = url.replaceAll("host", host).replaceAll("port", port).replaceAll("dbName", dbName);
        String driverName = "com.mysql.jdbc.Driver";
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(jdbcUrl, userName, password);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }


    /**
     * 根据sql语句执行
     *
     * @param
     * @param sql
     * @return
     * @throws SQLException
     */
    public static int execAction(String dbName1, String host1, String port1, String userName1, String password1, String sql) throws SQLException {
        dbName = dbName1;
        host = host1;
        port = port1;
        userName = userName1;
        password = password1;
        if (conn == null) {
            getJdbcConn();
            statement = conn.createStatement();
        }
        int count = 0;
        count = statement.executeUpdate(sql);
        log.info("【执行的sql：{},count: {}】", sql, count);
        if (count > 0) {
            log.info("【mysql 操作成功】===================================");
        }
        return count;
    }

    /**
     * 查询一个字段
     */
    public static String execSelectResult(String dbName1, String host1, String port1, String userName1, String password1, String sql) throws SQLException {
        dbName = dbName1;
        host = host1;
        port = port1;
        userName = userName1;
        password = password1;
        if (conn == null) {
            getJdbcConn();
            statement = conn.createStatement();
        }
        ResultSet resultSet = statement.executeQuery(sql);
        String result = "";
        while (resultSet.next()) {
            result = resultSet.getString(1);
        }
        return result;
    }

    /**
     * 关流
     */
    public static void close() {
        if (conn != null) {
            try {
//                if (pst != null) {
//                    pst.close();
//                }
                if (conn != null) {

                    conn.close();
                    conn = null;
                }
//                if (statement != null) {
//                    statement.close();
//                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
